package com.wandian.weather.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.wandian.weather.R;
import com.wandian.weather.tools.Lunar;
import com.wandian.weather.tools.wnl.OnWheelChangedListener;
import com.wandian.weather.tools.wnl.WheelView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class SelectDateDialog implements View.OnClickListener{

    private Activity mActivity;
    private View view;
    private Dialog shareDialog;
    private int mYear, mMonth, mDay;
    private WheelView wvYear;
    private WheelView wvMonth;
    private WheelView wvDay;
    private TextView tvLunar;
    private onSelectDate onSelectDate;

    private List<String> listYears = new ArrayList<>();
    private List<String> listMonths = new ArrayList<>();
    private List<String> listDays = new ArrayList<>();

    public SelectDateDialog(Activity activity) {
        this.mActivity = activity;
        onCreateDialog();
    }

    public void setOnSelectDate(SelectDateDialog.onSelectDate onSelectDate) {
        this.onSelectDate = onSelectDate;
    }

    protected void onCreateDialog() {
        view = LayoutInflater.from(mActivity).inflate(R.layout.weather_dialog_calendar_select_date, null);
        shareDialog = new Dialog(mActivity, R.style.CustomStyle);
        shareDialog.setCanceledOnTouchOutside(false);
        Window dialogWindow = shareDialog.getWindow();
        dialogWindow.setWindowAnimations(android.R.style.Animation_InputMethod);

        wvYear = (WheelView) view.findViewById(R.id.wheel1);
        wvMonth = (WheelView) view.findViewById(R.id.wheel2);
        wvDay = (WheelView) view.findViewById(R.id.wheel3);
        TextView tvCancle =(TextView) view.findViewById(R.id.tv_close);
        TextView tvDone =(TextView) view.findViewById(R.id.tv_done);
        tvLunar =(TextView) view.findViewById(R.id.tv_lunar);
        tvCancle.setOnClickListener(this);
        tvDone.setOnClickListener(this);

        initDialogData();

        shareDialog.setContentView(view);
        Window window = shareDialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);//去除dialog默认边距
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(windowParams);
        window.setGravity(Gravity.BOTTOM);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_close) {
            shareDialog.dismiss();
        } else if (id == R.id.tv_done) {
            if (onSelectDate != null)
                onSelectDate.showSelectDate(mYear, mMonth, mDay);
            shareDialog.dismiss();
        }
    }

    private void initDialogData() {
        wvYear.setEntries(getYears());
        wvMonth.setEntries(getMonths());
//        wvDay.setEntries(getDays());

        wvYear.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView view, int oldIndex, int newIndex) {
                String text = (String) wvYear.getItem(newIndex);
                mYear = Integer.parseInt(text);
                updateDayEntries();
                showLunar();
            }
        });

        wvMonth.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldIndex, int newIndex) {
                String text = (String) wvMonth.getItem(newIndex);
                mMonth = Integer.parseInt(text);
                updateDayEntries();
                showLunar();
            }
        });

        wvDay.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldIndex, int newIndex) {
                String text = (String) wvDay.getItem(newIndex);
                mDay = Integer.parseInt(text);
                showLunar();
            }
        });


        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH) + 1;
        mDay = calendar.get(Calendar.DAY_OF_MONTH);

        for (int i = 0; i < listYears.size(); i++) {
            String str = listYears.get(i);
            if (TextUtils.equals(str, String.valueOf(mYear))) {
                wvYear.setCurrentIndex(i);
                break;
            }
        }

        for (int i = 0; i < listMonths.size(); i++) {
            String str = listMonths.get(i);
            if (TextUtils.equals(str, String.valueOf(mMonth))) {
                wvMonth.setCurrentIndex(i);
                break;
            }
        }

        for (int i = 0; i < listDays.size(); i++) {
            String str = listDays.get(i);
            if (TextUtils.equals(str, String.valueOf(mDay))) {
                wvDay.setCurrentIndex(i);
                break;
            }
        }
    }

    private List<String> getYears(){
        listYears.clear();
        for(int i=1990; i<=2049; i++){
            listYears.add(String.valueOf(i));
        }
        return listYears;
    }

    private List<String> getMonths(){
        listMonths.clear();
        for(int i=1; i<=12; i++){
            listMonths.add(String.valueOf(i));
        }
        return listMonths;
    }

    private void updateDayEntries() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, mYear);
        calendar.set(Calendar.MONTH, mMonth - 1);

        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        listDays.clear();
        switch (days) {
            case 28:
                for(int i=1; i<=28; i++){
                    listDays.add(String.valueOf(i));
                }
                break;
            case 29:
                for(int i=1; i<=29; i++){
                    listDays.add(String.valueOf(i));
                }
                break;
            case 30:
                for(int i=1; i<=30; i++){
                    listDays.add(String.valueOf(i));
                }
                break;
            case 31:
            default:
                for(int i=1; i<=31; i++){
                    listDays.add(String.valueOf(i));
                }
                break;
        }
        wvDay.setEntries(listDays);
    }

    private void showLunar(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, mYear);
        calendar.set(Calendar.MONTH, mMonth - 1);
        calendar.set(Calendar.DAY_OF_MONTH, mDay);
        Lunar lunar = new Lunar(calendar);
        tvLunar.setText("农历 "+lunar.toString2());
    }

    public void showDialog() {
        shareDialog.show();
    }

    public interface onSelectDate{
        void showSelectDate(int year, int month, int day);
    }
}
